{% extends 'home.html' %}
{% block content %}

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>电位滴定仪</title>
    <link href="/static/css/kfc_yb.css" rel="stylesheet">
</link>
    <script src="/static/bootstrap/js/jquery.min.js" type="text/javascript"></script>
    <script src="/static/js/dwy.js" type="text/javascript"></script>
</head>
    <style>
        /* 保留其他样式不变 */
        table#tools input,
        table#tools select {
            width: 95% !important;
        }
    </style>
</head>

<body>
    <form name="form1" method="post" id="form1">
        {% csrf_token %}
        <div class="c-layout-page">
            <div class="c-content-box c-size-md c-bg-white">
                <div class="container">
                    <div class="c-content-title-1">
                        <h3 class="c-center c-font-dark c-font-uppercase">电位滴定仪校准</h3>
                        <div class="c-line-center c-theme-bg"></div>
                        <p class="c-center"></p>
                    </div>

                    <!-- 仪器基础信息输入 -->
                    <div class="c-content-panel">
                        <div class="c-label">仪器基础信息输入</div>
                        <div class="c-body">
                            <div class="row">
                                <div class="col-md-12">
                                    <table class="table" id="tools">
                                        <tbody>
                                            <tr>
                                                <th scope="row" style="text-align:center">仪器名称</th>
                                                <td colspan="2"><input name="A1PH_Name01" type="text" value="电位滴定仪"
                                                        id="A1PH_Name01" style="height:25px;width:350px"></td>
                                                <th style="text-align:center">仪器接收编号</th>
                                                <td colspan="2"><input name="A1PH_Received02" type="text"
                                                        value="{{ A1PH_Received02 }}" readonly="readonly"
                                                        id="A1PH_Received02"
                                                        style="background-color:#F3F3F3;height:25px; width:300px">
                                                </td>
                                            </tr>

                                            <tr>
                                                <th scope="row" style="text-align:center">型号/规格</th>
                                                <td><input name="A1PH_ProdModel03" type="text" id="A1PH_ProdModel03"
                                                        style="height:25px;width:240px"></td>
                                                <th style="text-align:center">仪器序列号</th>
                                                <td><input name="A1PH_SN04" type="text" id="A1PH_SN04"
                                                        style="height:25px">
                                                </td>
                                                <!-- ... existing code ... -->
                                                <th style="text-align:center">环境条件</th>                               
                                                <td>
                                                    <input name="A1PH_Wd11" type="text" id="A1PH_Wd11"
                                                        style="height:25px; width:50px !important;"
                                                        onblur="formatToOneDecimal(this)">&nbsp;°C &nbsp;&nbsp;
                                                    <input name="A1PH_Sd12" type="text" id="A1PH_Sd12"
                                                        style="height:25px; width:50px !important;"
                                                        onblur="formatToOneDecimal(this)">&nbsp;%RH
                                                    <script>
                                                        function formatToOneDecimal(inputElement) {
                                                            if (inputElement.value !== '') {
                                                                const value = parseFloat(inputElement.value);
                                                                if (!isNaN(value)) {
                                                                    inputElement.value = value.toFixed(1);
                                                                }
                                                            }
                                                        }

                                                        // 页面加载时也格式化已有的值
                                                        document.addEventListener('DOMContentLoaded', function () {
                                                            formatToOneDecimal(document.getElementById('A1PH_Wd11'));
                                                            formatToOneDecimal(document.getElementById('A1PH_Sd12'));
                                                        });
                                                    </script>
                                                </td>
                                                <!-- ... existing code ... -->
                                            </tr>
                                            <tr>
                                                <th style="text-align:center" scope="row">校准依据</th>
                                                <td colspan="3"><input name="A1PH_Xz06" type="text"
                                                        value="JJG 658-2022电位滴定仪检定规程" id="A1PH_Xz06"
                                                        style="height:25px;width:420px"></td>
                                                <th style="text-align:center">校准日期</th>
                                                <td>
                                                    <input name="A1PH_Rq07" type="text" value="{{ date }}"
                                                        readonly="readonly" id="A1PH_Rq07"
                                                        style="background-color: #f3f3f3; height: 25px; width: 120px" />
                                                </td>
                                            </tr>

                                            <tr>
                                                <td colspan="6"
                                                    style="height:20px;text-align:center;background-color:aliceblue">
                                                    标准器及配套设备
                                                </td>
                                            </tr>

                                            <tr>
                                                <td colspan="6">
                                                    <table class="table" id="tools">
                                                        <tbody>
                                                            <tr>

                                                                <th style="text-align:center">名称/型号</th>
                                                                <th style="text-align:center">编号</th>
                                                                <th style="text-align:center">测量范围</th>
                                                                <th style="text-align:center">批号/证书编号</th>
                                                                <th style="text-align:center">有效期</th>
                                                                <th style="text-align:center">校准机构</th>
                                                                <th style="text-align:center">计量特性</th>
                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh16" type="text"
                                                                        id="BLPH_Xmxh16" value="/" style="height:25px">
                                                                </td>
                                                                <td><input name="BLPH_Bh17" id="BLPH_Bh17"value="/"
                                                                        style="height:30px;width:107px;"></td>
                                                                     
                                                                <td><input name="BLPH_FW_01" type="text" id="BLPH_FW_01"
                                                                        value="/" style="height:25px;width:100px"></td>
                                                                <td><input name="BLPH_Jlzsbh18" type="text" value="/"
                                                                        id="BLPH_Jlzsbh18" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq19" type="text"
                                                                        id="BLPH_Yxrq19" value="/" style="height:25px">
                                                                </td>
                                                                <td><input name="BLPH_Yzjg20" type="text"
                                                                        id="BLPH_Yzjg20" value="/"
                                                                        style="height:25px;width:100px"></td>
                                                                <td><input name="BLPH_Zqddj21" type="text"
                                                                        id="BLPH_Zqddj21" value="/"
                                                                        style="height:25px; width:100px"></td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh22" type="text" value="/"
                                                                        id="BLPH_Xmxh22" style="height:25px"></td>
                                                                <td><input name="BLPH_Bh23" id="BLPH_Bh23" value="/"
                                                                      style="height:30px;width:107px;">
                                                                        </td>
                                                                <td><input name="BLPH_FW_02" type="text" value="/"
                                                                        id="BLPH_FW_02" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh24" type="text" value="/"
                                                                        id="BLPH_Jlzsbh24" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq25" type="text" value="/"
                                                                        id="BLPH_Yxrq25" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg26" type="text" value="/"
                                                                        id="BLPH_Yzjg26"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj27" type="text" value="/"
                                                                        id="BLPH_Zqddj27"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh28" type="text" value="/"
                                                                        id="BLPH_Xmxh28" style="height:25px"></td>
                                                                <td><input name="BLPH_Bh29" id="BLPH_Bh29" value="/"
                                                                        style="height:30px;width:107px;">

                                                                <td><input name="BLPH_FW_03" type="text" value="/"
                                                                        id="BLPH_FW_03" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh30" type="text" value="/"
                                                                        id="BLPH_Jlzsbh30" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq31" type="text" value="/"
                                                                        id="BLPH_Yxrq31" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg32" type="text" value="/"
                                                                        id="BLPH_Yzjg32"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj33" type="text" value="/"
                                                                        id="BLPH_Zqddj33"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh34" type="text" value="/"
                                                                        id="BLPH_Xmxh34" style="height:25px"></td>
                                                                <td><input name="BLPH_Bh35" id="BLPH_Bh35" value="/"
                                                                        style="height:30px;width:107px;">

                                                                <td><input name="BLPH_FW_04" type="text" value="/"
                                                                        id="BLPH_FW_04" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh36" type="text" value="/"
                                                                        id="BLPH_Jlzsbh36" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq37" type="text" value="/"
                                                                        id="BLPH_Yxrq37" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg38" type="text" value="/"
                                                                        id="BLPH_Yzjg38"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj39" type="text" value="/"
                                                                        id="BLPH_Zqddj39"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh40" type="text" value="/"
                                                                        id="BLPH_Xmxh40" style="height:25px"></td>
                                                                <td><input name="BLPH_Bh41" id="BLPH_Bh41" value="/"
                                                                        style="height:30px;width:107px;">


                                                                    </td>
                                                                <td><input name="BLPH_FW_05" type="text" value="/"
                                                                        id="BLPH_FW_05" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh42" type="text" value="/"
                                                                        id="BLPH_Jlzsbh42" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq43" type="text" value="/"
                                                                        id="BLPH_Yxrq43" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg44" type="text" value="/"
                                                                        id="BLPH_Yzjg44"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj45" type="text" value="/"
                                                                        id="BLPH_Zqddj45"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td><input name="BLPH_Xmxh46" type="text" value="/"
                                                                        id="BLPH_Xmxh46" style="height:25px"></td>
                                                                <td><input name="BLPH_Bh47" id="BLPH_Bh47" value="/"
                                                                        style="height:30px;width:107px;">

                                                                <td><input name="BLPH_FW_06" type="text" value="/"
                                                                        id="BLPH_FW_06" style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Jlzsbh48" type="text" value="/"
                                                                        id="BLPH_Jlzsbh48" style="height:25px"></td>
                                                                <td><input name="BLPH_Yxrq49" type="text" value="/"
                                                                        id="BLPH_Yxrq49" style="height:25px"></td>
                                                                <td><input name="BLPH_Yzjg50" type="text" value="/"
                                                                        id="BLPH_Yzjg50"
                                                                        style="height:25px;width:100px">
                                                                </td>
                                                                <td><input name="BLPH_Zqddj51" type="text" value="/"
                                                                        id="BLPH_Zqddj51"
                                                                        style="width:100px;height:25px">
                                                                </td>

                                                            </tr>
                                                            <tr>
                                                                <td>生产厂商</td>
                                                                <td colspan="6"><input name="A1PH_Sccs52" type="text"
                                                                        value="梅特勒托利多" id="A1PH_Sccs52"
                                                                        style="width:900px;height:25px"></td>
                                                            </tr>
                                                            <tr>
                                                                <td>证书单位</td>
                                                                <td colspan="6"><input name="A1PH_Zsdw53" type="text"
                                                                        id="A1PH_Zsdw53" 
                                                                        style="width:900px;height:25px"></td>
                                                            </tr>
                                                            <tr>
                                                                <td>证书地址</td>
                                                                <td colspan="6"><input name="A1PH_Zsdz54" type="text"
                                                                        id="A1PH_Zsdz54"
                                                                        style="width:900px;height:25px"></td>
                                                            </tr>
                                                            <tr>
                                                                <td>校准地点</td>
                                                                <td colspan="6"><input name="A1PH_Xzdd55" type="text"
                                                                        id="A1PH_Xzdd55"
                                                                        style="width:900px;height:25px"></td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 电计性能及仪器示值部分 -->
                    <div class="c-content-panel">
                        <div class="c-label">一、电计性能及仪器示值</div>
                        <div class="c-body" id="sensor-container">
                        </div>

                        <!-- 滴定管容量误差部分 -->
                        <div class="c-content-panel">
                            <div class="c-label">二、滴定管</div>
                            <div class="c-body" id="ddg-container">
                                <!-- 滴定管部分将在这里动态加载 -->
                            </div>
                        </div>

                        <!-- 本次校准结果的扩展不确定度 -->
                        <div class="c-content-panel">
                            <div class="c-label">本次校准结果的扩展不确定度 (<i>k</i>=2)</div>
                            <div class="c-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="table-responsive">
                                            <table class="table">
                                                <tbody>
                                                    <tr>
                                                        <th colspan="2">电计示值误差 <i>U</i>= <input name="K0" type="text" id="K0"
                                                                value="0.5" style="width:110px; height:25px">
                                                            mV
                                                        </th>
                                                    </tr>
                                                    <tr>
                                                        <th colspan="2">滴定管容量误差 <i>U</i>= <input name="K1" type="text" id="K1"
                                                                value="0.01" style="width:110px; height:25px">
                                                            mL
                                                        </th>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="table-responsive">
                                            <table class="table table-bordered">
                                                <tbody>
                                                    <tr>
                                                        <th>校准员：</th>
                                                        <td><input name="calibrator" type="text" id="calibrator"
                                                                style="width:110px; height:25px;border-width: 0.5px; border-style: inset; background-color: #F3F3F3"
                                                                value="{{ windows_user }}"></td>
                                                        <th>核验员</th>
                                                        <td><input name="inspector_Users" type="text"
                                                                id="inspector_Users" style="width:110px; height:25px">
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                            <!-- 将原来第646行左右的提交按钮代码替换为： -->
                                            <div class="S001">
                                                <br>
                                                <table>
                                                    <tbody>
                                                        <!-- <tr>
                                                            <td colspan="5" align="center">
                                                                <input type="submit" name="Button2" value="提交校准"
                                                                    onclick="javascript:if(Button2_onclick()==false) return false;"
                                                                    id="Button2" style="width:80px;">
                                                            </td>

                                                        </tr> -->

                                                        <tr>
                                                            <td colspan="5" align="center">
                                                                <a href="/list/" class="btn btn-default btn-xs" style="margin-top: 10px;width: 80px;">取  消</a>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    </form>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-body">
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                    <h4>注意!</h4>
                    <p>请确认相关数据已经填写正确，提交后无法修改！！！</p>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                        </button>
                        <button type="button" class="btn btn-danger" onclick="confirmAdd();">
                            确定提交
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
{% endblock %}

{% block js %}
<script src="/static/bootstrap/js/jquery.min.js" type="text/javascript"></script>
<script>
       $(document).ready(function () {
            var data_obj = {{ obj | safe }}
            console.log("接收到的数据:", data_obj);

            // 填充基础信息
            $.each(data_obj, function (key, value) {
                // 跳过 sensor_data 和 ddg_data，因为它们需要特殊处理
                if (key === 'sensor_data' || key === 'ddg_data') {
                    return;
                }
                
                // 处理普通输入框
                var inputSelector = $('input[name="' + key + '"]');
                if (inputSelector.length) {
                    inputSelector.val(value);
                    inputSelector.attr("readonly", "readonly");
                }

                // 处理下拉框
                var selectSelector = $('select[name="' + key + '"]');
                if (selectSelector.length) {
                    selectSelector.val(value);
                    selectSelector.attr("disabled", "disabled");
                }
            });

            // 特殊处理
            $('#submit').attr("disabled", "disabled");
            $('select').attr("disabled", "disabled");
            $('input').attr("disabled", "disabled");

            // 解析并加载传感器和滴定管数据
            loadSensorAndDdgData(data_obj);
        });

        function loadSensorAndDdgData(data_obj) {
            console.log("开始加载传感器和滴定管数据...");
            
            // 解析传感器数据
            let sensors = [];
            if (data_obj.sensor_data) {
                try {
                    sensors = JSON.parse(data_obj.sensor_data);
                } catch (e) {
                    console.error("解析传感器数据失败:", e);
                }
            } else if (data_obj.sensor && Array.isArray(data_obj.sensor)) {
                sensors = data_obj.sensor;
            }
            
            // 加载传感器数据
            if (Array.isArray(sensors)) {
                loadSensorData(sensors);
            }

            // 解析滴定管数据
            let ddgs = [];
            if (data_obj.ddg_data) {
                try {
                    ddgs = JSON.parse(data_obj.ddg_data);
                } catch (e) {
                    console.error("解析滴定管数据失败:", e);
                }
            } else if (data_obj.ddg && Array.isArray(data_obj.ddg)) {
                ddgs = data_obj.ddg;
            }
            
            // 加载滴定管数据
            if (Array.isArray(ddgs)) {
                loadDdgData(ddgs);
            }
        }

        // 其余函数保持不变
        function loadSensorData(sensors) {
            console.log("加载传感器数据:", sensors);
            const container = $('#sensor-container');
            container.empty();

            sensors.forEach((sensorData, index) => {
                const sensorIndex = index + 1;
                console.log(`处理传感器${sensorIndex}:`, sensorData);

                const panel = $(`
                    <div class="sensor-panel" id="sensor-panel-${sensorIndex}">
                        <div class="c-content-panel">
                            <div class="c-label">${sensorIndex}、电极接口 SENSOR${sensorIndex}</div>
                            <div class="c-body sensor-content" id="sensor-content-${sensorIndex}">
                                <!-- 传感器内容将通过ajax加载 -->
                            </div>
                        </div>
                    </div>
                `);
                container.append(panel);

                // 异步加载传感器内容
                loadSensorContent(sensorIndex, sensorData);
            });
        }

        function loadSensorContent(sensorIndex, sensorData) {
            console.log(`加载传感器内容，索引: ${sensorIndex}`, sensorData);
            $.ajax({
                url: '/static/temp/sensor.html',
                type: 'GET',
                success: function (data) {
                    console.log(`传感器模板加载成功，索引: ${sensorIndex}`);
                    // 创建一个临时容器来处理HTML
                    const tempDiv = $('<div>').html(data);

                    // 修改input元素
                    tempDiv.find('input').each(function () {
                        const originalName = $(this).attr('name');
                        if (originalName) {
                            // 构造带索引的ID
                            const indexedId = originalName + '_sensor' + sensorIndex;
                            $(this).attr('id', indexedId);
                            $(this).attr('data-sensor-index', sensorIndex);
                            $(this).attr('data-original-name', originalName);
                            
                            // 从传感器数据中获取值
                            const value = sensorData[originalName];
                            if (value !== undefined) {
                                $(this).val(value);
                            }
                            
                            // 设置为只读
                            $(this).attr('readonly', 'readonly');
                            console.log(`传感器${sensorIndex} - 输入框: ${originalName}, 值: ${value}`);
                        }
                    });

                    // 修改select元素
                    tempDiv.find('select').each(function () {
                        const originalName = $(this).attr('name');
                        if (originalName) {
                            // 构造带索引的ID
                            const indexedId = originalName + '_sensor' + sensorIndex;
                            $(this).attr('id', indexedId);
                            $(this).attr('data-sensor-index', sensorIndex);
                            $(this).attr('data-original-name', originalName);
                            
                            // 从传感器数据中获取值
                            const value = sensorData[originalName];
                            if (value !== undefined) {
                                $(this).val(value);
                            }
                            
                            // 设置为禁用
                            $(this).attr('disabled', 'disabled');
                            console.log(`传感器${sensorIndex} - 下拉框: ${originalName}, 值: ${value}`);
                        }
                    });

                    // 将处理后的内容放入对应的容器
                    $(`#sensor-content-${sensorIndex}`).html(tempDiv.html());
                    
                    // 在内容加载完成后，再次确保所有字段都被正确填充
                    setTimeout(function() {
                        fillSensorData(sensorIndex, sensorData);
                    }, 100);
                    
                    console.log(`传感器内容已加载到页面，索引: ${sensorIndex}`);
                },
                error: function (xhr, status, error) {
                    console.error(`加载传感器模板失败，索引: ${sensorIndex}`, error);
                    $(`#sensor-content-${sensorIndex}`).html('<p>加载电极模板失败</p>');
                }
            });
        }

        function fillSensorData(sensorIndex, sensorData) {
            // 再次遍历所有字段并填充数据
            $.each(sensorData, function(key, value) {
                const elementId = key + '_sensor' + sensorIndex;
                const element = $('#' + elementId);
                if (element.length) {
                    if (element.is('input')) {
                        element.val(value);
                        element.attr('readonly', 'readonly');
                    } else if (element.is('select')) {
                        element.val(value);
                        element.attr('disabled', 'disabled');
                    }
                    console.log(`再次填充传感器${sensorIndex} - ${key}: ${value}`);
                }
            });

            // 确保所有传感器输入都被禁用
            $(`#sensor-content-${sensorIndex} input`).attr("disabled", "disabled");
        }

        function loadDdgData(ddgs) {
            console.log("加载滴定管数据:", ddgs);
            const container = $('#ddg-container');
            container.empty();

            ddgs.forEach((ddgData, index) => {
                const ddgIndex = index + 1;
                console.log(`处理滴定管${ddgIndex}:`, ddgData);

                const panel = $(`
                    <div class="ddg-panel" id="ddg-panel-${ddgIndex}">
                        <div class="c-content-panel">
                            <div class="c-label">${ddgIndex}、滴定管</div>
                            <div class="c-body ddg-content" id="ddg-content-${ddgIndex}">
                                <!-- 滴定管内容将通过ajax加载 -->
                            </div>
                        </div>
                    </div>
                `);
                container.append(panel);

                // 异步加载滴定管内容
                loadDdgContent(ddgIndex, ddgData);
            });
        }

        function loadDdgContent(ddgIndex, ddgData) {
            console.log(`加载滴定管内容，索引: ${ddgIndex}`, ddgData);
            $.ajax({
                url: '/static/temp/ddg.html',
                type: 'GET',
                success: function (data) {
                    console.log(`滴定管模板加载成功，索引: ${ddgIndex}`);
                    // 创建一个临时容器来处理HTML
                    const tempDiv = $('<div>').html(data);

                    // 修改input元素
                    tempDiv.find('input').each(function () {
                        const originalName = $(this).attr('name');
                        if (originalName) {
                            // 构造带索引的ID
                            const indexedId = originalName + '_ddg' + ddgIndex;
                            $(this).attr('id', indexedId);
                            $(this).attr('data-ddg-index', ddgIndex);
                            $(this).attr('data-original-name', originalName);
                            
                            // 从滴定管数据中获取值
                            const value = ddgData[originalName];
                            if (value !== undefined) {
                                $(this).val(value);
                            }
                            
                            // 设置为只读
                            $(this).attr('readonly', 'readonly');
                            console.log(`滴定管${ddgIndex} - 输入框: ${originalName}, 值: ${value}`);
                        }
                    });

                    // 修改select元素
                    tempDiv.find('select').each(function () {
                        const originalName = $(this).attr('name');
                        if (originalName) {
                            // 构造带索引的ID
                            const indexedId = originalName + '_ddg' + ddgIndex;
                            $(this).attr('id', indexedId);
                            $(this).attr('data-ddg-index', ddgIndex);
                            $(this).attr('data-original-name', originalName);
                            
                            // 从滴定管数据中获取值
                            const value = ddgData[originalName];
                            if (value !== undefined) {
                                $(this).val(value);
                            }
                            
                            // 设置为禁用
                            $(this).attr('disabled', 'disabled');
                            console.log(`滴定管${ddgIndex} - 下拉框: ${originalName}, 值: ${value}`);
                        }
                    });

                    // 将处理后的内容放入对应的容器
                    $(`#ddg-content-${ddgIndex}`).html(tempDiv.html());
                    
                    // 在内容加载完成后，再次确保所有字段都被正确填充
                    setTimeout(function() {
                        fillDdgData(ddgIndex, ddgData);
                    }, 100);
                    
                    console.log(`滴定管内容已加载到页面，索引: ${ddgIndex}`);
                },
                error: function (xhr, status, error) {
                    console.error(`加载滴定管模板失败，索引: ${ddgIndex}`, error);
                    $(`#ddg-content-${ddgIndex}`).html('<p>加载滴定管模板失败</p>');
                }
            });
        }

        function fillDdgData(ddgIndex, ddgData) {
            // 再次遍历所有字段并填充数据
            $.each(ddgData, function(key, value) {
                const elementId = key + '_ddg' + ddgIndex;
                const element = $('#' + elementId);
                if (element.length) {
                    if (element.is('input')) {
                        element.val(value);
                        element.attr('readonly', 'readonly');
                    } else if (element.is('select')) {
                        element.val(value);
                        element.attr('disabled', 'disabled');
                    }
                    console.log(`再次填充滴定管${ddgIndex} - ${key}: ${value}`);
                }
            });

            // 确保所有滴定管输入都被禁用
            $(`#ddg-content-${ddgIndex} input`).attr("disabled", "disabled");
        }

</script>

{% endblock %}